# -*- coding: gbk -*-
'''
使用方法:在命令行中传递主机报表所在的文件夹即可,会在脚本当前目录生成结果。
'''
import codecs
import re
import os
import sys
def progress(width,precent):
#显示百分比与进度表
print "%d%% [%s]\r" % (precent,('%%-%ds' % width) % (width * precent / 100 * '=')),
if precent >= 100:
sys.stdout.flush()
def flaw_high(html):
#从html中匹配出高风险的漏洞名称
pattern = re.compile(r'<a href="#[\S]*" class="vul-vh">([\w\W]*?)</a>')
match = pattern.findall(html)
return match
def flaw_middle(html):
#从html中匹配出中风险的漏洞名称
pattern = re.compile(r'<a href="#[\S]*" class="vul-vm">([\w\W]*?)</a>')
match = pattern.findall(html)
return match
def EachHost(file_route,save_route):
'''
读取主机报表file_route文件,并保存到save_route文件
'''
#file_route = "1.html"
file = codecs.open(file_route,"r","utf-8")
contents = file.read()
#print contents
pattern = re.compile(r'</td><td>([\d]+.[\d]+.[\d]+.[\d]+)</td></tr>')
ip = pattern.findall(contents) #从主机报表中获取主机IP,并写进文件
#print ip[0]
f_high = flaw_high(contents)
file_forwrite = codecs.open(save_route,"a","utf-8")
file_forwrite.write(ip[0])
for i_ in f_high:
file_forwrite.write(u',高风险,'+i_+'\r\n')
f_middle = flaw_middle(contents)
for i_ in f_middle:
file_forwrite.write(u',中风险,'+i_+'\r\n')
if len(f_high)==0 and len(f_middle)==0:
file_forwrite.write('\r\n')
file_forwrite.close()
file.close()
def ListFiles(dir_route,save_route):
'''
从主机报表路径dir_route中遍历主机报表,并保存到save_route
'''
total = 0
for i in os.listdir(dir_route): #遍历html文件,即所有主机报表
if i.endswith("html"):
total += 1
i = 0
for i_file in os.listdir(dir_route):#将所有主机报表逐一处理,并计算百分比
if i_file.endswith("html"):
i += 1
rate_num = int((float(i)/float(total))*100)
progress(50,rate_num)
EachHost(dir_route + "\\" + i_file,save_route)
if __name__ == '__main__':
'''
主函数,简单判断输入是否正确
'''
#EachHost("1.html")
#print sys.path[0]
save_route = ""
while len(save_route)==0 and len(sys.argv)>1:
print u"请输入生成文件名:"
save_route = raw_input()
save_route = sys.path[0]+"\\"+save_route+".csv"
if os.path.exists(save_route):
print u"该文件名已存在,请重新输入!!!"
save_route = ""
print save_route
if len(sys.argv)>1:
ListFiles(sys.argv[1],save_route)
else:
print u'请输入主机报表路径!!!\r\n如:D:\\script.py D:\\scan_report\\hosts'